*test version includes a measure on exporting side for share of dollar for competitors

clear
set more 1
set type double

capture log using "log/competitor_currency.log", replace
*****************************************************************
*NOTES:
*construct a measure of share of noneuro of competitors in destination country-HS4 
*1. dollar vs other ../data/mcompetitor_xcurrency_dollar
*2. euro vs other ../data/mcompetitor_xcurrency
*3. importing side: euro vs other ../data/mcompetitor_mcurrency
*******************************************************************************************

**************************************************	
*1. time invariant currency choice - dollar vs other
**********************************************
*i. at vat-destination level
*********************************************
 use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
     	drop if missing(currency)
      	drop *I
      	drop if missing(valueX)
      	rename valueX value
	rename country cty_code
 	 	
 	gen hs4=substr(cncode,1,4)
       	
    	
*only keep manuf firms
***************************************
merge m:1 vat using ../data/manuf_firm, keep(match) nogen
	gen other=1 if currency=="USD"
	replace other=0 if missing(other)
    	
    	gen value_other=value*other
    	
    	collapse (sum) value value_other, by(vat cty_code hs4 month year)
    	*gen date = ym(year, month)
    	*format date %tm
    	
    	egen totvalue_other=sum(value_other), by(cty_code hs4 month year)
      	egen totvalue=sum(value), by(cty_code hs4 month year)
   	
    	gen shcompetitor_dollar= (totvalue_other - value_other)/(totvalue-value)
      	gen shfirm_dollar= value_other/value
    	
    	keep year month vat cty_code hs4 sh*
      	save ../data/competitor_xcurrency_t_dollar, replace
    	
    	collapse (mean) mshcompetitor_dollar=shcompetitor_dollar, by(vat cty_code hs4)
      	save ../data/mcompetitor_xcurrency_dollar, replace
   
		
**************************************************	
*2. time invariant currency choice - EURO vs other
**********************************************
*i. at vat-destination level
*********************************************
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
       drop *I
       rename valueX value
       drop if missing(value)
       rename country cty_code
       drop if missing(currency)
  	 	
 	gen hs4=substr(cncode,1,4)
    	
*only keep manuf firms
***************************************
merge m:1 vat using ../data/manuf_firm, keep(match) nogen
	gen other=1 if currency~="EUR"
	replace other=0 if missing(other)
    	
    	gen value_other=value*other
    	
    	collapse (sum) value value_other, by(vat cty_code hs4 month year)
    	*gen date = ym(year, month)
    	*format date %tm
    	
    	egen totvalue_other=sum(value_other), by(cty_code hs4 month year)
      	egen totvalue=sum(value), by(cty_code hs4 month year)
    	
    	gen shcompetitor_other= (totvalue_other - value_other)/(totvalue-value)
      	gen shfirm_other= value_other/value
    	
    	keep year month vat cty_code hs4 sh*
      	save ../data/competitor_xcurrency_t, replace
    	
    		collapse (mean) mshcompetitor_other=shcompetitor_other, by(vat cty_code hs4)
      	save ../data/mcompetitor_xcurrency, replace
    	    	
    	
 ******************************************************************************************
 *IMPORTING SIDE
 ******************************************************************************************
 
  use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
 	rename valueI value
 	drop if missing(value)
 	rename country cty_code
 	drop if missing(currency)
	drop if cty_code=="EU"
	gen hs4=substr(cncode,1,4)
     	
 *only keep manuf firms
 ***************************************
 merge m:1 vat using ../data/manuf_firm, keep(match) nogen
 	gen other=1 if currency~="EUR"
 	replace other=0 if missing(other)
     	
     	gen value_other=value*other
     	
     	collapse (sum) value value_other, by(vat cty_code hs4 month year)
     	     	
     	egen totvalue_other=sum(value_other), by(cty_code hs4 month year)
       	egen totvalue=sum(value), by(cty_code hs4 month year)
     	
     	gen shcompetitor_other= (totvalue_other - value_other)/(totvalue-value)
       	gen shfirm_other= value_other/value
     	
     	keep year month vat cty_code hs4 sh*
       	save ../data/competitor_mcurrency_t, replace
     	
     		collapse (mean) mshcompetitor_other=shcompetitor_other, by(vat cty_code hs4)
       	save ../data/mcompetitor_mcurrency, replace	
     	
 
